/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import pojo.ChiTietHoaDon;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class ChiTietHoaDonDAO {
    public static List<ChiTietHoaDon> layDanhSachChiTietHoaDon() {
        List<ChiTietHoaDon> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select cthd from ChiTietHoaDon cthd";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }   
        return ds;        
    }
    
    public static ChiTietHoaDon layThongTinHoaDon(int maCTHoaDon) {
        ChiTietHoaDon cthd = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            cthd = (ChiTietHoaDon) session.get(ChiTietHoaDon.class, maCTHoaDon);
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return cthd;
    }
    
    public static void themChiTietHoaDon(ChiTietHoaDon cthd) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(cthd);
        session.getTransaction().commit();
        session.close(); 
    }
}
